<?php namespace shared\model\material;
/**
 * @subpackage  \shared\model\material
 *
 * @copyright   Copyright (C) 2013 - 2014 Mailerm, Inc. All rights reserved.
 */

 # Load Libraries
require_once MPATH_LIBRARIES . '/database/MySQL.php';

use \MySQL as MySQL;

# Load Configuration
require_once MPATH_MODULES . '/configuration.php';

// Count materials
// @return count
function count($project) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    # Count All
    // SELECT
  
    $database->prepare('SELECT COUNT(`id`) AS \'count\' FROM `' . TBL_MATERIAL . '` WHERE `status`!=? AND `project`=?');
    $database->execute(MATERIAL_STATUS_DELETED, $project);
    $results = $database->results();    
    # Num Rows
    if($database->num_rows() < 1) {
        return -1;
    }
    return intval($results[0]['count']);
}

// Find available materials
// @return array
function find($project, $start, $size) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
	$database->prepare('SELECT `id`,`project`,`status`,`name`,`class`,`type`,`model`,`unit`,`count`,`stock`,`used`,`transport`,`budge_price`,`planed` FROM `' . TBL_MATERIAL . '` WHERE `status`!=? AND `project`=? LIMIT ?,?');
    $database->execute(MATERIAL_STATUS_DELETED, $project, $start, $size);
    $users = $database->results();
    return $users;
}